package com.ck.express.interceptor

import android.util.Log
import okhttp3.Interceptor
import okhttp3.Response
import okio.Buffer

// 日志拦截器 - LoggingInterceptor.kt
class LoggingInterceptor : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        val request = chain.request()

        // 记录请求日志
        Log.d("API Request", "URL: ${request.url}")
        Log.d("API Request", "Method: ${request.method}")
        request.body?.let {
            val buffer = Buffer()
            it.writeTo(buffer)
            Log.d("API Request", "Body: ${buffer.readUtf8()}")
        }

        val response = chain.proceed(request)

        // 记录响应日志
        Log.d("API Response", "Code: ${response.code}")
        response.body?.let {
            val source = it.source()
            source.request(Long.MAX_VALUE)
            val buffer = source.buffer.clone()
            Log.d("API Response", "Body: ${buffer.readUtf8()}")
        }

        return response
    }
}